package com.prs.crm.action.project.perusal;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.flow.FlowVerifyDetail;
import com.prs.crm.domain.project.Task;
import com.prs.crm.domain.project.TaskPerusal;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.project.TaskPerusalService;
import com.prs.crm.service.project.TaskService;

public class TaskPerusalAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private TaskService taskService;

	private TaskPerusalService taskPerusalService;
	
	private FlowVerifyDetail detail;
	
	private PaginationSupport pagination;
	
	private File upload;
	
	private String uploadFileName;	
	
	private TaskPerusal perusal;
	
	private boolean audited;
	
	private Task task;
	
	private String recivers;

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskPerusalService(TaskPerusalService taskPerusalService) {
		this.taskPerusalService = taskPerusalService;
	}

	public TaskPerusalService getTaskPerusalService() {
		return taskPerusalService;
	}

	public void setDetail(FlowVerifyDetail detail) {
		this.detail = detail;
	}

	public FlowVerifyDetail getDetail() {
		return detail;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setPerusal(TaskPerusal perusal) {
		this.perusal = perusal;
	}

	public TaskPerusal getPerusal() {
		return perusal;
	}

	public void setAudited(boolean audited) {
		this.audited = audited;
	}

	public boolean isAudited() {
		return audited;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Task getTask() {
		return task;
	}
	
	
	public void setRecivers(String recivers) {
		this.recivers = recivers;
	}

	public String getRecivers() {
		return recivers;
	}
	
	@Action("listTaskForPerusal")
	public String listTaskForPerusal()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Task.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTaskService().listExecuteTask(criteria, pagination,
				AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}
	
	@Action("posePerusal")
	public String posePerusal()
	{
		return SUCCESS;
	}
	
	@Action(value="createTaskPerusal",results={@Result(name="success",type="redirect",location="listTaskForPerusal")
			,	@Result(name="error",type="chain",location="posePerusal")})
	public String createTaskPerusal()
	{
		try
		{
			this.getTaskPerusalService().createTaskPerusal(perusal,recivers, audited);
			this.setPrompt("创建成功");
			return SUCCESS;
		}
		catch (InValidException e)
		{
			this.setTask(this.getTaskService().get(perusal.getTask().getId()));
			this.addActionError(e.getMessage());
			return ERROR;
		}
	}
	
	@Action("listVerifyTaskPerusal")
	public String listVerifyTaskPerusal()
	{
		this.setPagination(this.getTaskPerusalService().listProcessEntitys(this.getPaginationHelper().prepare(pagination)
							, AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}
	
	
	@Action("verifyTaskPerusal")
	public String verifyTaskPerusal()
	{
		this.setTask(perusal.getTask());
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="saveTaskPerusalVerify",results={@Result(name="success",type="redirect",location="listVerifyTaskPerusal")})
	public String saveTaskPerusalVerify()
	{
		this.getTaskPerusalService().executeVerify(perusal, detail,
					AppContext.getAccountContext().currentUser(), upload, uploadFileName);
		this.setPrompt("审核成功");
		return SUCCESS;
	}
	
	@Action("listVerifyPerusalHistory")
	public String listVerifyPerusalHistory()
	{
		this.setPagination(this.getTaskPerusalService().listVerifyPersualHistory(this.getPaginationHelper().prepare(pagination)
				, AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}
	@Action("taskPerusalInfo")
	public String taskPerusalInfo()
	{
		this.setTask(perusal.getTask());
		return SUCCESS;
	}
	
	@Action("listTaskPerusalVerifyDetail")
	public String listTaskPerusalVerifyDetail()
	{
		return SUCCESS;
	}
	
	@Action("viewTaskPerusalVerifyDetail")
	public String viewTaskPerusalVerifyDetail()
	{
		return SUCCESS;
	}

	/**
	 * 查看传阅给我的任务列表
	 * @return
	 */
	@Action("listPerusalTask")
	public String listPerusalTask()
	{
		this.setPagination(this.getTaskPerusalService().listPerusalTask(this.getPaginationHelper().prepare(pagination)
				, AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}
	
	
	/**
	 * 查看传阅给我的任务明细
	 * @return
	 */
	@Action("perusalTaskInfo")
	public String perusalTaskInfo()
	{
		return SUCCESS;
	}

}
